import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:ride_rex/generated/assets.dart';
import 'package:ride_rex/main/mine/article/mine_article_logic.dart';
import 'package:ride_rex/service/user_service.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

import '../../../article/post/article_post_bean.dart';
import '../../../standard/app_colors.dart';

class MineArticlePage extends GetView<MineAticalLogic> {
  const MineArticlePage({super.key});

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      titleStr: 'My Article',
      scaffoldBackgroundColor: Colors.white,
      child: GetBuilder<MineAticalLogic>(
        init: MineAticalLogic(),
        builder: (logic) {
          if (logic.list.isEmpty) return _buildEmpty();
          return GridView.builder(
            padding: EdgeInsets.only(top: 24.w, left: 20.w, right: 20.w),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 10.w,
              crossAxisSpacing: 10.w,
              childAspectRatio: 0.7,
            ),
            itemBuilder: (context, index) {
              PostArticleBean bean = logic.list[index];
              return MineAticalItem(
                bean,
                onDelete: () => logic.delete(bean.recordId ?? ''),
              );
            },
            itemCount: logic.list.length,
          );
        },
      ),
    );
  }

  Widget _buildEmpty() {
    return Align(
      alignment: const Alignment(0, -0.3),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          IconWidget.image(Assets.pngAticalEmpty, width: 189.w, height: 193.w),
          TextWidget.oneLine(text: 'Start your creation.', fontSize: 16, color: Colors.black.withOpacity(0.5)),
        ],
      ),
    );
  }
}

class MineAticalItem extends StatelessWidget {
  const MineAticalItem(this.bean, {super.key, this.onDelete, this.onTap});

  final VoidCallback? onDelete;
  final VoidCallback? onTap;
  final PostArticleBean bean;

  @override
  Widget build(BuildContext context) {
    String cover = '';
    bool exist = false;
    if (bean.cover?.isNotEmpty ?? false) {
      cover = bean.cover!.first;
    }
    if ((bean.localFilePath ?? []).isNotEmpty) {
      cover = bean.localFilePath!.first;
      exist = File(cover).existsSync();
    }

    Widget child = Container(
      height: 170.w,
      color: Colors.black.withOpacity(0.1),
    );

    if (cover.startsWith('http')) {
      child = ImageWidget.url(cover, fit: BoxFit.cover, height: 170.w);
    } else if (cover.startsWith('assets')) {
      child = ImageWidget.asset(cover, fit: BoxFit.cover, height: 170.w);
    } else {
      if (exist) child = ImageWidget.file(cover, fit: BoxFit.cover, height: 170.w);
    }

    return ButtonWidget(
      hasInkWell: false,
      onTap: onTap,
      child: Stack(
        alignment: Alignment.topRight,
        children: [
          Container(
            decoration: CommonDecoration.getShadow(
              radius: 8.w,
              shadowColor: Colors.black.withOpacity(0.1),
              borderColor: Colors.transparent,
            ),
            clipBehavior: Clip.hardEdge,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                FractionallySizedBox(widthFactor: 1, child: child),
                TextWidget.oneLine(text: bean.title, fontSize: 10, color: AppColors.text).paddingAll(10.w),
                Row(
                  children: [
                    ImageWidget.url(
                      UserService.instance.notifierOfMineInfo.value.avatarThumbUrl ?? '',
                      width: 20.w,
                      radius: 20.w,
                    ),
                    TextWidget.oneLine(
                      text: UserService.instance.notifierOfMineInfo.value.nickname ?? '',
                      fontSize: 10,
                      color: AppColors.text,
                    ).paddingHorizontal(8.w).expanded(),
                    // Icon(Icons.handshake_rounded, size: 12.w),
                  ],
                ).paddingHorizontal(10.w),
              ],
            ),
          ),
          if (onDelete != null)
            ButtonWidget.icon(
              IconWidget.image(Assets.pngDelete, size: 24.w),
              onTap: onDelete,
              hasInkWell: false,
            ).positioned(top: 4.w, right: 4.w),
        ],
      ),
    );
  }
}
